#!/usr/bin/env python
""" AnyBackup is a windows specific backup program to keep multiple volumes in sync with multiple volumes
    Copyright (C) 2011  Andrew Hopkinson

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Exported Classes:

    FileUtil -- used to determine file types and provide icons for those file types it also manages the image lists for drive windows and contains an eror icon for error messages displayed in the results listctrl
    """

import wx
import re

class FileUtil:
    """ used to determine file types and provide icons for those file types it also manages the image lists for drive windows and contains an eror icon for error messages displayed in the results listctrl """
    def __init__(self):
        wx.InitAllImageHandlers()
        folder = wx.Bitmap("icons/folder_icon.png",wx.BITMAP_TYPE_PNG)
        video = wx.Bitmap("icons/video_icon.png",wx.BITMAP_TYPE_PNG)
        audio = wx.Bitmap("icons/audio_icon.png",wx.BITMAP_TYPE_PNG)
        archive = wx.Bitmap("icons/archive_icon.png",wx.BITMAP_TYPE_PNG)
        book = wx.Bitmap("icons/book_icon.png",wx.BITMAP_TYPE_PNG)
        file = wx.Bitmap("icons/file_icon.png",wx.BITMAP_TYPE_PNG)
        image = wx.Bitmap("icons/image_icon.png",wx.BITMAP_TYPE_PNG)
        error = wx.Bitmap("icons/error_icon.png",wx.BITMAP_TYPE_PNG)
        self.windowLogo = wx.Icon("icons/windowlogo.png",wx.BITMAP_TYPE_PNG)
        self.imageList = wx.ImageList(16,16,True)
        self.typeIndex = {}
        self.typeIndex['folder'] = self.imageList.Add(folder)
        self.typeIndex['video'] = self.imageList.Add(video)
        self.typeIndex['audio'] = self.imageList.Add(audio)
        self.typeIndex['archive'] = self.imageList.Add(archive)
        self.typeIndex['book'] = self.imageList.Add(book)
        self.typeIndex['file'] = self.imageList.Add(file)
        self.typeIndex['image'] = self.imageList.Add(image)
        self.typeIndex['error'] = self.imageList.Add(error)
        self.driveImage = wx.ImageList(16,16,True)
        drive = wx.Bitmap("icons/drive_icon.png",wx.BITMAP_TYPE_PNG)
        driveCon = wx.Bitmap("icons/drivecon_icon.png",wx.BITMAP_TYPE_PNG)
        driveDis = wx.Bitmap("icons/drivedis_icon.png",wx.BITMAP_TYPE_PNG)
        self.driveLock = wx.Bitmap("icons/drivelock_icon.png",wx.BITMAP_TYPE_PNG)
        self.logo = wx.Bitmap("icons/backup.png",wx.BITMAP_TYPE_PNG)
        self.driveIndex = {}
        self.driveIndex['drivecon']=self.driveImage.Add(driveCon)
        self.driveIndex['drivedis']=self.driveImage.Add(driveDis)
        self.driveIndex['drivelock']=self.driveImage.Add(self.driveLock)
        self.driveIndex['drive']=self.driveImage.Add(drive)
        
    def getLockedBMP(self):
        return self.driveLock
    
    def getLogo(self):
        return self.logo

    def getConnected(self):
        return self.driveIndex['drivecon']

    def getDisconnected(self):
        return self.driveIndex['drivedis']
    
    def getLocked(self):
        return self.driveIndex['drivelock']

    def getWindowIcon(self):
        return self.windowLogo

    def getError(self):
        return self.typeIndex['error']

    def getIcon(self,file):
        if self.isDirectory(file):
            return self.typeIndex['folder']
        elif self.isVideo(file):
            return self.typeIndex['video']
        elif self.isArchive(file):
            return self.typeIndex['archive']
        elif self.isAudio(file):
            return self.typeIndex['audio']
        elif self.isBook(file):
            return self.typeIndex['book']
        elif self.isImage(file):
            return self.typeIndex['image']
        else:
            return self.typeIndex['file']

    def getDriveList(self):
        return self.driveImage

    def getImageList(self):
        return self.imageList

    def isDirectory(self,file):
        if not file:
            return False
        if file.isDir():
            return True
        return False

    def isImage(self,file):
        if not file:
            return False
        if re.search("\.(png|jpg|bmp|ico|jpeg|gif|tif)$",file.getName(),re.IGNORECASE):
            return True
        return False

    def isVideo(self,file):
        if not file:
            return False
        if re.search("\.(avi|mkv|ogm|ts|rm|mp4|mpeg|mpg|divx|xvid)$",file.getName(),re.IGNORECASE):
            return True
        return False

    def isArchive(self,file):
        if not file:
            return False
        if re.search("\.(zip|rar|ace|7z|tar|gz|iso)$",file.getName(),re.IGNORECASE):
            return True
        return False

    def isAudio(self,file):
        if not file:
            return False
        if re.search("\.(mp3|oga|wav|aac|ac3|ra|flac)$",file.getName(),re.IGNORECASE):
            return True
        return False

    def isBook(self,file):
        if not file:
            return False
        if re.search("\.(prc|azw|mobi|epub|pdf|lit)$",file.getName(),re.IGNORECASE):
            return True
        return False